package com.example.util;

import lombok.Getter;
import lombok.Setter;

import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Setter
@Getter
public class UploadFileUtil {
    // 设置part
    private Part part; // 包含文件的part
    // 设置文件存放的位置（文件夹）
    private String path; // 文件存放的位置


	public String getFilename() { // 获取文件名
		String contentDispositionHeader = part.getHeader("content-disposition");
		System.out.println("contentDispositionHeader:::" + contentDispositionHeader);
		String[] elements = contentDispositionHeader.split(";");
		for (String element : elements) {
			if (element.trim().startsWith("filename")) {
				System.out.println(element);
				String filename = element.substring(element.indexOf('=') + 1).trim().replace("\"", "");
				int n = filename.lastIndexOf('\\');
				filename = filename.substring(n + 1);
				return filename;
			}
		}
		return null;
	}

	public int saveFile() { // 将文件保存在所设置的文件夹中
		// Collection<String> headerNames = part.getHeaderNames();
		// for (String header : headerNames) {
		// System.out.println(header + "=" + part.getHeader(header));
		// }
		try {
			InputStream is = part.getInputStream();
			String filename = new String(getFilename().getBytes(), StandardCharsets.UTF_8);
			File file = new File(path);
			if (!file.exists() || !file.isDirectory()) {
				file.mkdir();
			}
			System.out.println("kkkk"  +file + File.separator + filename);
			FileOutputStream fos = new FileOutputStream(file + File.separator + filename);
			byte[] buf = new byte[1024];
			while (is.read(buf) != -1) {
				fos.write(buf);
			}
			fos.flush();
			fos.close();
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}
}
